import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/widgets.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'gt_rect_swiper_pagination_builder.dart';

/// @Title   item为组件的banner（指示器在内部）
/// @Author: bryce
/// @CreateDate: 2023年08月31日15:04:01
class GTWidgetInnerBanner extends StatelessWidget {
  /// 宽/高比例
  final double aspectRatio;

  /// 根据index创建item
  final IndexedWidgetBuilder itemBuilder;

  /// banner页数
  final int itemCount;

  /// 默认第几页
  final int index;

  /// 是否自动滚动
  final bool autoplay;

  /// 是否循环滚动
  final bool loop;

  /// 自动滚动时间间隔 (in millisecond).
  final int autoplayDelay;

  /// 页面切换事件
  final ValueChanged<int>? onIndexChanged;

  /// 点击事件
  final SwiperOnTap? onTap;

  /// 控制器
  final SwiperController? controller;

  /// 是否显示分页指示器（只有在不止一页时这个属性才生效，如果只有1条那么即使这个为true也是不显示的）
  final bool paginationVisible;

  //默认true选中为黑色指示器，false选中为白色指示器
  final bool? isDark;
  //指示器是否在外部，默认在内部
  final bool? isOuter;

  const GTWidgetInnerBanner(
      {super.key,
      required this.aspectRatio,
      required this.itemCount,
      required this.itemBuilder,
      this.controller,
      this.onIndexChanged,
      this.onTap,
      this.autoplay = true,
      this.loop = true,
      this.paginationVisible = true,
      this.autoplayDelay = 3000,
      this.index = 0,
      this.isDark = true,
      this.isOuter});

  @override
  Widget build(BuildContext context) {
    if (itemCount == 0) return Container();
    Color activeColor;
    if (isDark ?? true) {
      activeColor = GTColorsV3.icon1.color;
    } else {
      activeColor = GTColorsV3.icon6.color;
    }
    return AspectRatio(
      aspectRatio: aspectRatio,
      child: ClipRRect(
          borderRadius: BorderRadius.circular(4),
          child: Swiper(
            index: index,
            outer: isOuter ?? false,
            autoplay: itemCount == 1 ? false : autoplay,
            autoplayDelay: autoplayDelay,
            loop: itemCount == 1 ? false : loop,
            itemCount: itemCount,
            itemBuilder: itemBuilder,
            onIndexChanged: onIndexChanged,
            onTap: onTap,
            axisDirection: GTThemeProvider.instance.isRTL() ? AxisDirection.left : AxisDirection.right,
            pagination: (itemCount == 1 || !paginationVisible)
                ? null
                : SwiperPagination(
                    margin: const EdgeInsets.all(8),
                    builder: GTRectSwiperPaginationBuilder(
                        activeColor: activeColor,
                        color: GTColorsV3.icon4.color,
                        space: 4,
                        activeSize: const Size(12, 4),
                        size: const Size(4, 4))),
            controller: controller,
          )),
    );
  }
}
